package com.sdjz.club.club.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdjz.club.club.entity.Favorite;
import com.sdjz.club.club.mapper.FavoriteMapper;
import com.sdjz.club.club.service.impl.FavoriteServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Dftre
 * @since 2022-08-06
 */
@RestController
@RequestMapping("/club/favorite")
public class FavoriteController {

    @Autowired
    FavoriteMapper favoriteMapper;
    @Autowired
    FavoriteServiceImp favoriteServiceImp;

    @RequestMapping("/select")
    public CommonResult select(
            @RequestParam(name = "userId") Long userId,
            @RequestParam(name = "type") String type
    ) {
        if (type.equals("organization")) {
            return CommonResult.succ(
                    favoriteMapper.getOrganizationByUserId(userId)
            );
        } else if (type.equals("event")) {
            return CommonResult.succ(
                    favoriteMapper.getEventByUserId(userId)
            );
        } else {

            return CommonResult.fail("参数错误");
        }
    }

    @PostMapping("/add")
    public CommonResult add(@RequestBody Favorite favorite) {
        return CommonResult.succ(favoriteMapper.insert(favorite));
    }

    @PostMapping("/update")
    public CommonResult update(@RequestBody Favorite favorite) {
        return CommonResult.succ(favoriteMapper.updateById(favorite));
    }

    @PostMapping("/delete")
    public CommonResult delete(
            @RequestParam(name = "userId") Long userId,
            @RequestParam(name = "collect") Long collect,
            @RequestParam(name = "type") String type
    ) {
        QueryWrapper<Favorite> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId)
                .eq("collect", collect)
                .eq("type", type);
        return CommonResult.succ(favoriteMapper.delete(wrapper));
    }

    @GetMapping("/have")
    public CommonResult have(
            @RequestParam(name = "userId") Long userId,
            @RequestParam(name = "collect") Long collect,
            @RequestParam(name = "type") String type
    ) {
        QueryWrapper<Favorite> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId)
                .eq("collect", collect)
                .eq("type", type);
        return CommonResult.succ(favoriteServiceImp.count(wrapper));
    }
}

